package com.china08.yxyapi.api.manage.edu.platform.statistics.resourcepandect;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.statistics.AreaResourceReport;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.statistics.AreaResourceReportRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 资源统计 趋势变化
 * 
 */
@CrossOrigin
@RestController
public class ResourceTrendChangeController extends YxyApiBaseController {
	@Autowired
	private AreaResourceReportRepository areaResourceReportRepository;

	@ApiOperation(value = "资源统计    趋势变化")
	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/edu/platform/statistics/resourcepandect/trendchange/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req) {
		RespModel model = new RespModel();
		List<AreaResourceReport> areaResourceReportLst = new ArrayList<AreaResourceReport>();

		// 资源上传数
		List<Long> rescountLst = new ArrayList<Long>();
		// 资源共享数量
		List<Long> shareCountLst = new ArrayList<Long>();
		//一周的年月日
		List<String> weekList=new ArrayList<String>();
		//一个月的年月日
	    List<String> monthList=new ArrayList<String>();
		if (req.dateValue.equals("一周")) {
			Integer schoolBureauLevel =getProfile(request, response).getSchoolBureauLevel();
			String districtCode = null;
			if (schoolBureauLevel == 0) {// 区
				districtCode = getProfile(request, response).getSchoolAreaIdDistrict();
//				areaResourceReportLst = areaResourceReportRepository
			    Long count=0L;
			    Long countShare=0L;
				ArrayList<Integer> oldDate = getOldDate(7 );
				for (Integer integer : oldDate) {
					String dateStr=String.valueOf(integer);
					String strDate=dateStr.substring(0, 4)+"-"+dateStr.substring(4, 6)+"-"+dateStr.substring(6, 8);
					weekList.add(strDate);
				}
				for (Integer oldDates : oldDate) {
					areaResourceReportLst=areaResourceReportRepository.findByAreaIdDistrictAndIntDate(districtCode, oldDates);
					if (areaResourceReportLst.size()>0) {
						for (AreaResourceReport areaResourceReport : areaResourceReportLst) {
							count+=areaResourceReport.getResCount();
							countShare+=areaResourceReport.getShareResCount();
						}
					}else{
						count=0L;
						countShare=0L;
					}
					
					rescountLst.add(count);
					shareCountLst.add(countShare);
					count=0L;
					countShare=0L;
				}
			} else if (schoolBureauLevel == 1) {// 市
				districtCode = getProfile(request, response).getSchoolAreaIdCity();
				/*areaResourceReportLst = areaResourceReportRepository
						.findByAreaIdCityAndStatisticsDateGreaterThan(districtCode, predictDate);*/
				 Long count=0L;
				 Long countShare=0L;
					ArrayList<Integer> oldDate = getOldDate(7);
					for (Integer integer : oldDate) {
						String dateStr=String.valueOf(integer);
						String strDate=dateStr.substring(0, 4)+"-"+dateStr.substring(4, 6)+"-"+dateStr.substring(6, 8);
						weekList.add(strDate);
					}
					for (Integer oldDates : oldDate) {
						areaResourceReportLst=areaResourceReportRepository.findByAreaIdCityAndIntDate(districtCode, oldDates);
						if (areaResourceReportLst.size()>0) {
							for (AreaResourceReport areaResourceReport : areaResourceReportLst) {
								count+=areaResourceReport.getResCount();
								countShare+=areaResourceReport.getShareResCount();
							}
						}else{
							count=0L;
							countShare=0L;
						}
						
						rescountLst.add(count);
						shareCountLst.add(countShare);
						count=0L;
						countShare=0L;
					}
			} else if (schoolBureauLevel == 2) {// 省
				districtCode = getProfile(request, response).getSchoolAreaIdProvince();
				/*areaResourceReportLst = areaResourceReportRepository
						.findByAreaIdProvinceAndStatisticsDateGreaterThan(districtCode, predictDate);*/
				Long count=0L;
				Long countShare=0L;
				ArrayList<Integer> oldDate = getOldDate(7);
				for (Integer integer : oldDate) {
					String dateStr=String.valueOf(integer);
					String strDate=dateStr.substring(0, 4)+"-"+dateStr.substring(4, 6)+"-"+dateStr.substring(6, 8);
					weekList.add(strDate);
				}
				for (Integer oldDates : oldDate) {
					areaResourceReportLst=areaResourceReportRepository.findByAreaIdProvinceAndIntDate(districtCode, oldDates);
					if (areaResourceReportLst.size()>0) {
						for (AreaResourceReport areaResourceReport : areaResourceReportLst) {
							count+=areaResourceReport.getResCount();
							countShare+=areaResourceReport.getShareResCount();
						}
					}else{
						count=0L;
						countShare=0L;
					}
					
					rescountLst.add(count);
					shareCountLst.add(countShare);
					count=0L;
					countShare=0L;
				}
			}
			
		}
		if (req.dateValue.equals("一个月")) {
			Integer schoolBureauLevel =getProfile(request, response).getSchoolBureauLevel();
			String districtCode = null;
			if (schoolBureauLevel == 0) {// 区
				districtCode = getProfile(request, response).getSchoolAreaIdDistrict();
//				areaResourceReportLst = areaResourceReportRepository
			    Long count=0L;
			    Long countShare=0L;
				ArrayList<Integer> oldDate = getOldDate(30);
				for (Integer integer : oldDate) {
					String dateStr=String.valueOf(integer);
					String strDate=dateStr.substring(0, 4)+"-"+dateStr.substring(4, 6)+"-"+dateStr.substring(6, 8);
					monthList.add(strDate);
				}
				for (Integer oldDates : oldDate) {
					areaResourceReportLst=areaResourceReportRepository.findByAreaIdDistrictAndIntDate(districtCode, oldDates);
					if (areaResourceReportLst.size()>0) {
						for (AreaResourceReport areaResourceReport : areaResourceReportLst) {
							count+=areaResourceReport.getResCount();
							countShare+=areaResourceReport.getShareResCount();
						}
					}else{
						count=0L;
						countShare=0L;
					}
					
					rescountLst.add(count);
					shareCountLst.add(countShare);
					count=0L;
					countShare=0L;
				}
			} else if (schoolBureauLevel == 1) {// 市
				districtCode = getProfile(request, response).getSchoolAreaIdCity();
				/*areaResourceReportLst = areaResourceReportRepository
						.findByAreaIdCityAndStatisticsDateGreaterThan(districtCode, predictDate);*/
				 Long count=0L;
				 Long countShare=0L;
					ArrayList<Integer> oldDate = getOldDate(30);
					for (Integer integer : oldDate) {
						String dateStr=String.valueOf(integer);
						String strDate=dateStr.substring(0, 4)+"-"+dateStr.substring(4, 6)+"-"+dateStr.substring(6, 8);
						monthList.add(strDate);
					}
					for (Integer oldDates : oldDate) {
						areaResourceReportLst=areaResourceReportRepository.findByAreaIdCityAndIntDate(districtCode, oldDates);
						if (areaResourceReportLst.size()>0) {
							for (AreaResourceReport areaResourceReport : areaResourceReportLst) {
								count+=areaResourceReport.getResCount();
								countShare+=areaResourceReport.getShareResCount();
							}
						}else{
							count=0L;
							countShare=0L;
						}
						
						rescountLst.add(count);
						shareCountLst.add(countShare);
						count=0L;
						countShare=0L;
					}
			} else if (schoolBureauLevel == 2) {// 省
				districtCode = getProfile(request, response).getSchoolAreaIdProvince();
				/*areaResourceReportLst = areaResourceReportRepository
						.findByAreaIdProvinceAndStatisticsDateGreaterThan(districtCode, predictDate);*/
				Long count=0L;
				Long countShare=0L;
				ArrayList<Integer> oldDate = getOldDate(30);
				for (Integer integer : oldDate) {
					String dateStr=String.valueOf(integer);
					String strDate=dateStr.substring(0, 4)+"-"+dateStr.substring(4, 6)+"-"+dateStr.substring(6, 8);
					monthList.add(strDate);
				}
				for (Integer oldDates : oldDate) {
					areaResourceReportLst=areaResourceReportRepository.findByAreaIdProvinceAndIntDate(districtCode, oldDates);
					if (areaResourceReportLst.size()>0) {
						for (AreaResourceReport areaResourceReport : areaResourceReportLst) {
							count+=areaResourceReport.getResCount();
							countShare+=areaResourceReport.getShareResCount();
						}
					}else{
						count=0L;
						countShare=0L;
					}
					
					rescountLst.add(count);
					shareCountLst.add(countShare);
					count=0L;
					countShare=0L;
				}
			}
		
		}

		model.rescountLst = rescountLst;
		model.shareCountLst = shareCountLst;
		model.weekList=weekList;
		model.monthList=monthList;
		return model;
	}

	protected static class RespModel extends YxyApiRespModel {
		// 资源上传数
		public List<Long> rescountLst;
		// 资源共享数量
		public List<Long> shareCountLst;
		// 一周的年月日
	    public List<String> weekList;
	    // 一个月的年月日
		public List<String> monthList;
	}

	protected static class ReqModel extends YxyApiReqModel {
		// 一周或者是一个月
		public String dateValue;
	}
	// 获取过去N天的时间
    public static ArrayList<Integer> getOldDate(int intervals) {
        ArrayList<Integer> pastDaysList = new ArrayList<>();
        for (int i = intervals-1; i >= 0 ; i--) {
            pastDaysList.add(Integer.parseInt(getPastDate(i)));
        }
        return pastDaysList;
    }

    // 获取过去N天的时间
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String result = format.format(today);
        return result;
    }
}
